package com.ruoyi.mall.product.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.ruoyi.mall.product.domain.entity.PmsBrand;
import com.ruoyi.mall.product.domain.entity.PmsCategory;
import com.ruoyi.mall.product.domain.entity.PmsCategoryBrandRelation;
import com.ruoyi.mall.product.domain.repository.PmsBrandMapper;
import com.ruoyi.mall.product.domain.repository.PmsCategoryBrandRelationMapper;
import com.ruoyi.mall.product.domain.repository.PmsCategoryMapper;
import com.ruoyi.mall.product.domain.vo.PmsBrandVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @program: zqs-mall
 * @description:
 * @author: Mr.Zhang
 * @create: 2022-06-08 17:19
 **/
@Service
public class PmsBrandCategoryRelationAppService {
    @Autowired
    private PmsCategoryBrandRelationMapper pmsCategoryBrandRelationMapper;
    @Autowired
    private PmsCategoryMapper pmsCategoryMapper;
    @Autowired
    private PmsBrandMapper pmsBrandMapper;

    /**
     * 根据品牌id查询关联分类
     *
     * @param brandId
     * @return
     */
    public List<PmsCategoryBrandRelation> selectByBrandId(Long brandId) {
        QueryWrapper<PmsCategoryBrandRelation> wrapper = new QueryWrapper<>();
        wrapper.eq("brand_id", brandId);
        return pmsCategoryBrandRelationMapper.selectList(wrapper);
    }

    /**
     * 保存
     *
     * @param pmsCategoryBrandRelation
     * @return
     */
    public int insertPmsCategoryBrand(PmsCategoryBrandRelation pmsCategoryBrandRelation) {
        Long brandId = pmsCategoryBrandRelation.getBrandId();
        Long categoryId = pmsCategoryBrandRelation.getCategoryId();

        PmsBrand pmsBrand = pmsBrandMapper.selectPmsBrandById(brandId);
        pmsCategoryBrandRelation.setBrandName(pmsBrand.getName());
        PmsCategory pmsCategory = pmsCategoryMapper.selectById(categoryId);
        pmsCategoryBrandRelation.setCategoryName(pmsCategory.getName());

        return pmsCategoryBrandRelationMapper.insert(pmsCategoryBrandRelation);
    }

    /**
     * 关联品牌更新
     *
     * @param brandId
     * @param brandName
     */
    public void updateByBrandName(Long brandId, String brandName) {
        UpdateWrapper<PmsCategoryBrandRelation> wrapper = new UpdateWrapper<>();
        wrapper.eq("brand_id", brandId);
        PmsCategoryBrandRelation relation = new PmsCategoryBrandRelation();
        relation.setBrandName(brandName);
        relation.setBrandId(brandId);
        pmsCategoryBrandRelationMapper.update(relation, wrapper);
    }

    /**
     * 关联分类更新
     *
     * @param categoryId
     * @param categoryName
     */
    public void updateByCategoryName(Long categoryId, String categoryName) {
        UpdateWrapper<PmsCategoryBrandRelation> wrapper = new UpdateWrapper<>();
        wrapper.eq("category_id", categoryId);
        PmsCategoryBrandRelation relation = new PmsCategoryBrandRelation();
        relation.setCategoryName(categoryName);
        relation.setCategoryId(categoryId);
        pmsCategoryBrandRelationMapper.update(relation, wrapper);
    }

    /**
     * 删除分类关系
     *
     * @param id
     * @return
     */
    public int deleteById(Long id) {
        return pmsCategoryBrandRelationMapper.deleteById(id);
    }

    /**
     * 查询当前分类的所属品牌
     *
     * @param catId
     * @return
     */
    public List<PmsBrand> selectByCatId(Long catId) {
        List<PmsCategoryBrandRelation> pmsCategoryBrandRelations = pmsCategoryBrandRelationMapper.selectList(new QueryWrapper<PmsCategoryBrandRelation>().eq("category_id", catId));
        List<Long> brandIds = pmsCategoryBrandRelations.stream().map(PmsCategoryBrandRelation::getBrandId).collect(Collectors.toList());

        List<PmsBrand> list = new ArrayList<>();
        brandIds.forEach(i -> {
            PmsBrand pmsBrand = pmsBrandMapper.selectById(i);
            list.add(pmsBrand);
        });
        return list;
    }
}
